package com.hp.attendance.serlvet.oneself;

import com.hp.attendance.entity.Attendance;
import com.hp.attendance.entity.Department;
import com.hp.attendance.entity.Employee;
import com.hp.attendance.entity.PageInfo;
import com.hp.attendance.entity.dto.AttDepEmpDto;
import com.hp.attendance.service.AttendanceService;
import com.hp.attendance.service.DepartmentService;
import com.hp.attendance.service.EmployeeService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/oneselfOvertime")
public class OneselfOvertimeServlet extends HttpServlet {
    private EmployeeService employeeService=new EmployeeService();
    private DepartmentService departmentService=new DepartmentService();
    private AttendanceService attendanceService=new AttendanceService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            Employee emp = (Employee) req.getSession().getAttribute("emp");
            Employee employee = employeeService.queryEmployee(emp.getId());
            List<Department> list = departmentService.getList();
            Department department1 = null;
            for (Department department : list) {
                if (department.getDepartment_number().equals(employee.getDepartment_number())){
                    department1=department;
                }
            }
            String pageNo = req.getParameter("pageNo");
            if(pageNo==null ||"".equals(pageNo.trim())){
                pageNo="1";
            }
            PageInfo<Attendance> page = attendanceService.getPage(employee.getEmployee_number(), Integer.parseInt(pageNo), 10);
            ArrayList<AttDepEmpDto> attDepEmpDtos = new ArrayList<>();
            List<Attendance> records = page.getRecords();
            for (Attendance record : records) {
                AttDepEmpDto attDepEmpDto = new AttDepEmpDto(record,department1,employee);
                attDepEmpDtos.add(attDepEmpDto);

            }
            PageInfo<AttDepEmpDto> page1 = new PageInfo<>();
            page1.setRecords(attDepEmpDtos);
            page1.setPages(page.getPages());
            page1.setCurrent(page.getCurrent());
            page1.setTotal(page.getTotal());
            req.setAttribute("page",page1);
            req.getRequestDispatcher("pages/oneself_overtime.jsp").forward(req,resp);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
